//
//  GanLuEditInfoVC.m
//  GanLuApp
//
//  Created by 暗流澎涌 on 2024/4/3.
//

#import "GanLuEditInfoVC.h"
#import "GanLudetailCell.h"
#import "GanLuHongFile.h"
#import "GanLuBiaoqingManager.h"
#import "GanLuXinQingSaver.h"
#import <AVKit/AVKit.h>
#import <IQKeyboardManager/IQKeyboardManager.h>


@interface GanLuEditInfoVC ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomDistance;
@property (strong, nonatomic) UIImage *imag;
@property (strong, nonatomic) GanLudetailCell *ganLudetailCell;
@property (strong, nonatomic) NSDate *cdate;
@property (weak, nonatomic) IBOutlet UIView *toolbal;


@end

@implementation GanLuEditInfoVC




- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GanLudetailCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([GanLudetailCell class])];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(up:) name:UIKeyboardWillShowNotification object:nil];//键盘将要出现时
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(down:) name:UIKeyboardWillHideNotification object:nil];//键盘将要收回时
    
    
    if(self.isEdit){
        
        NSDictionary *data = [GanLuXinQingSaver date:self.day];
        NSData *imgdata = data[@"img"];
        self.imag = [UIImage imageWithData:imgdata];
        self.cdate = data[@"time"];
        NSInteger typeTag = [data[@"tagType"] integerValue];

        self.typeTag = typeTag;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.ganLudetailCell.BQtextView.text = data[@"content"];
        });
    }
//    self.bottomDistance.constant = 233 + bottomLayoutIPhoneX + 35;
    
    if(!self.day){
        self.day = [NSDate date];
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.ganLudetailCell){
        self.ganLudetailCell.BQImage.image = self.imag;
        [self.ganLudetailCell setTime:self.cdate];
        return self.ganLudetailCell;
    }
    GanLudetailCell* cell = [self.tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GanLudetailCell class])];
    self.ganLudetailCell = cell;
    self.ganLudetailCell.BQImage.image = self.imag;
    cell.biaoqingIamge.image = [UIImage imageNamed:[GanLuBiaoqingManager backImgForBiaoqingTag:self.typeTag]];
    cell.biaoqingLabel.text = [GanLuBiaoqingManager backTitleForBiaoqingTag:self.typeTag];
    [cell setDayAndWeek:self.day];
    [self.ganLudetailCell setTime:self.cdate];
    [cell.addBtn addTarget:self action:@selector(clickAdd:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (IBAction)clickCLose:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)clickSUre:(id)sender {
    [GanLuXinQingSaver saveDate:self.day time:self.cdate img:self.imag tagType:self.typeTag content:self.ganLudetailCell.BQtextView.text];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)clickHistory:(id)sender {
    self.cdate = [NSDate date];
    [self.tableView reloadData];
}

- (void)down:(NSNotification *)info{
    [self.toolbal setHidden:YES];
}

- (void)up:(NSNotification *)info {
//    NSDictionary *userInfo = info.userInfo;
//    CGRect keyBoardFrame = [userInfo [UIKeyboardFrameBeginUserInfoKey] CGRectValue];//获取信息字典中的键盘的高
    CGRect keyBoardFrame = [[[info userInfo] valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    [self.toolbal setHidden:NO];
    if (@available(iOS 13.0, *)) {
        
        self.bottomDistance.constant = keyBoardFrame.size.height;
    } else {
        self.bottomDistance.constant = keyBoardFrame.size.height;
    }
    
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    [self dismissViewControllerAnimated:YES completion:nil];
    self.imag = image;
    [self.tableView reloadData];
}

- (IBAction)clickAdd:(id)sender {
    //初始化UIImagePickerController类
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}

@end
